package com.xxwu.springblogsystem.controller;

import com.xxwu.springblogsystem.Contants;
import com.xxwu.springblogsystem.model.Blog;
import com.xxwu.springblogsystem.model.Result;
import com.xxwu.springblogsystem.service.BlogService;
import com.xxwu.springblogsystem.utils.JWTUtils;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blogController")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<Blog> queryBlogList(){
        return blogService.queryBlogList();
    }

    @RequestMapping("/getBlogDetail")
    /**
     * 判断当前登录用户与博客创建者是否一致
     * 若一致才可删除，通过loginUser标志位实现
     */
    public Blog getBlogDetail(Integer id, HttpServletRequest request){
        //获取token
        String token = request.getHeader(Contants.USER_TOKEN);
        //对token进行解析
        Integer userId = JWTUtils.getUserIDFromToken(token);
        Blog blog = blogService.getBlogDetail(id);
        if(userId != null && userId == blog.getUserId()){
            blog.setLoginUser(1);
        }
        log.info("blogId值为：" + id);
        log.info("loginUser:" + blog.getLoginUser());
        return blog;
    }

    @RequestMapping("/add")
    public Result addBlog(String content, String title, HttpServletRequest request){
        //先通过token获取用户id
        log.info("请求头中的token" + request.getHeader("user_token"));
        Integer id = JWTUtils.getUserIDFromToken(request.getHeader("user_token"));
        log.info("id值为："+id);
        if(id == null){
            return Result.fail("用户未登录");
        }
        Blog blog = new Blog();
        blog.setContent(content);
        blog.setTitle(title);
        blog.setUserId(id);
        Integer result = blogService.addBlog(blog);
        if(result < 1){
            return Result.fail("插入失败");
        }
        return Result.success(result);
    }

    /**
     * 更新博客，此时不需要userId
     * @param id
     * @param title
     * @param content
     * @return
     */
    @RequestMapping("/update")
    public Result updateBlog(Integer blogId, String title, String content){
        if(blogId == null || !StringUtils.hasLength(title) || !StringUtils.hasLength(content)){
            return Result.fail("ID/title/content不合法");
        }
        Blog blog = new Blog();
        blog.setId(blogId);
        blog.setTitle(title);
        blog.setContent(content);
        Integer result = blogService.updateBlog(blog);
        if(result < 1){
            return Result.fail("插入失败");
        }
        return Result.success(result);
    }

    /**
     * 删除博客
     */
    @RequestMapping("/delete")
    public Result deleteBlog(Integer id){
        if(id == null){
            return Result.fail("id不合法");
        }
        Integer result = blogService.deleteBlog(id);
        if(result < 1){
            return Result.fail("删除失败");
        }
        return Result.success(result);
    }
}
